package com.jinjunhuang.memoryopt;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";

    private TextView showTv;

    private Button innerBtn;
    private Button handlerBtn;
    private Button handlerFitBtn;
    private Button oomBtn;
    private Button checkMemInfoBtn;
    private Button singleBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showTv = findViewById(R.id.show_tv);
        showTv.setText(Runtime.getRuntime().maxMemory() / 1024 / 1024 + " MB");

        innerBtn = findViewById(R.id.inner_btn);
        handlerBtn = findViewById(R.id.handler_btn);
        handlerFitBtn = findViewById(R.id.handler_fit_btn);
        oomBtn = findViewById(R.id.oom_btn);
        checkMemInfoBtn = findViewById(R.id.check_memory_used_btn);
        singleBtn = findViewById(R.id.single_btn);

        singleBtn.setOnClickListener(this);
        checkMemInfoBtn.setOnClickListener(this);
        innerBtn.setOnClickListener(this);
        handlerBtn.setOnClickListener(this);
        handlerFitBtn.setOnClickListener(this);
        oomBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.inner_btn: {
                intent.setClass(MainActivity.this, InnerActivity.class);
                break;
            }
            case R.id.handler_btn: {
                intent.setClass(MainActivity.this, HandlerActivity.class);
                break;
            }
            case R.id.handler_fit_btn: {
                intent.setClass(MainActivity.this, HandlerFitActivity.class);
                break;
            }
            case R.id.oom_btn: {
                int[] array = new int[1024 * 1024 * 1024];
                break;
            }
            case R.id.single_btn:{
                intent.setClass(MainActivity.this,SingleInstanceActivity.class);
                break;
            }
            case R.id.check_memory_used_btn: {
                ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningAppProcessInfo> appProcessList = activityManager.getRunningAppProcesses();
                for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
                    // 进程ID号
                    int pid = appProcessInfo.pid;
                    // 用户ID 类似于Linux的权限不同，ID也就不同 比如 root等
                    int uid = appProcessInfo.uid;
                    // 进程名，默认是包名或者由属性android：process=""指定
                    String processName = appProcessInfo.processName;
                    // 获得该进程占用的内存
                    int[] myMempid = new int[] { pid };
                    // 此MemoryInfo位于android.os.Debug.MemoryInfo包中，用来统计进程的内存信息
                    Debug.MemoryInfo[] memoryInfo = activityManager.getProcessMemoryInfo(myMempid);
                    // 获取进程占内存用信息 kb单位
                    int memSize = memoryInfo[0].dalvikPrivateDirty;

                    Log.i(TAG, "processName: " + processName + "  pid: " + pid
                            + " uid:" + uid + " memorySize is -->" + memSize + "kb");

                    // 获得每个进程里运行的应用程序(包),即每个应用程序的包名
                    String[] packageList = appProcessInfo.pkgList;
                    Log.i(TAG, "process id is " + pid + " has " + packageList.length);
                    for (String pkg : packageList) {
                        Log.i(TAG, "packageName " + pkg + " in process id is -->"+ pid);
                    }
                }
//                if (activityManager != null) {
//                    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
//                    activityManager.getMemoryInfo(memoryInfo);
//                    Toast.makeText(this, memoryInfo.availMem / 1024 / 1024 + "MB", Toast.LENGTH_SHORT).show();
//                }
                return;
            }
        }
        startActivity(intent);
    }

}
